<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>美兰数据文化馆与图书馆</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
</head>
<body>
<div class="layui-fluid" style="padding: 0px;">
    <div class="layui-card">
        <div class="layui-card-body">
            <blockquote class="layui-elem-quote">
                <div class="layui-btn-group">
                    <button class="layui-btn" id="btn-expand">全部展开</button>
                    <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                </div>
            </blockquote>
            <table class="layui-hide" id="menuTable" lay-filter="menuTables"></table>
        </div>
    </div>
</div>
<script type="text/html" id="menuDome">
    <shiro:hasPermission name="sys:menu:add">
        <div class="layui-inline" lay-event="add" title="添加">
            <i class="layui-icon layui-icon-add-1"></i>
        </div>
    </shiro:hasPermission>
</script>
<script type="text/html" id="table-menu-webuser">
    <shiro:hasPermission name="sys:menu:update">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update"></i>修改</a>
    </shiro:hasPermission>
    <shiro:hasPermission name="sys:menu:del">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </shiro:hasPermission>
    <%--    <shiro:lacksPermission name="sys:menu:update">--%>
    <%--        <a class="layui-btn layui-btn-disabled layui-btn-xs">修改</a>--%>
    <%--    </shiro:lacksPermission>--%>
    <%--    <shiro:lacksPermission name="sys:menu:del">--%>
    <%--        <a class="layui-btn layui-btn-disabled layui-btn-xs">删除</a>--%>
    <%--    </shiro:lacksPermission>--%>
</script>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['treetable', 'table', 'layer', 'laypage'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , layer = layui.layer,
            treetable = layui.treetable;

        var renderTable = function () {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'tid',
                treePidName: 'parentTid',
                title: "菜单列表",
                elem: '#menuTable',
                url: '<%=request.getContextPath()%>/rest/system/menuList',
                page: false,
                text: {
                    none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                toolbar: '#menuDome',
                defaultToolbar: ['filter', 'print'],
                cols: [[
                    {title: '序号', width: 70, sort: true, fixed: 'left', type: 'numbers'},
                    {field: 'title', minWidth: 200, title: '菜单名称'},
                    {field: 'perms', title: '权限标识'},
                    {field: 'href', title: '菜单url'},
                    {
                        field: 'type', title: '类型',
                        templet: function (d) {
                            if (d.type == 1) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else if (d.type == 2) {
                                return '<span class="layui-badge-rim">菜单</span>';
                            } else if (d.type == 3) {
                                return '<span class="layui-badge-rim layui-bg-red">按钮</span>';
                            } else {
                                return '<span class="layui-badge-rim layui-bg-green">系统</span>';
                            }
                        }
                    },
                    {
                        field: 'delfag', title: '状态',
                        templet: function (d) {
                            if (d.delflag == 1) {
                                return '<span class="layui-badge-rim">正常</span>';
                            } else {
                                return '<span class="layui-badge-rim layui-bg-red">已禁用</span>';
                            }
                        }
                    },
                    {field: 'sort', title: '排序', edit: 'text'},
                    {templet: '#table-menu-webuser', width: 120, align: 'center', title: '操作'}
                ]]
                , response: {
                    statusName: 'code' //规定数据状态的字段名称，默认：code
                    , statusCode: 0 //规定成功的状态码，默认：0
                    , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                    , countName: 'count' //规定数据总数的字段名称，默认：count
                    , dataName: 'data' //规定数据列表的字段名称，默认：data
                },
                done: function (res, curr, count) {
                    // alert(res.count)
                    console.log(res); //得到美化后的DOM对象
                    //关闭加载
                    layer.closeAll('loading');
                },

            });
        }

        renderTable();

        $('#btn-expand').click(function () {
            treetable.expandAll('#menuTable');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#menuTable');
        });

        //监听工具条
        table.on('tool(menuTables)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {
                del(data)
            } else if (layEvent === 'update') {
                // layer.msg('修改' + data.tid);
                update(data);
            }
        });

        table.on('toolbar(menuTables)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data;//获取选中的数据
            switch (obj.event) {
                case 'add':
                    var clocess = "1";
                    var index = layer.open({
                        title: "添加",
                        type: 2,
                        content: '<%=request.getContextPath()%>/rest/getRoute?type=system&url=menu_add',
                        maxmin: true,
                        area: ['90%', '91%'],
                        //是否显示右上角关闭图标，默认为1，显示
                        anim: 4,
                        shade: 0.6,
                        skin: 'layui-layer-molv',
                        resize: true,
                        cancel: function (index, layero) {
                            layer.close(index)
                            clocess = "2"
                            return false;
                        },
                        end: function () {
                            if (clocess == "2") {

                            } else {
                                renderTable();
                            }
                            return false;
                        }
                    });

                    // $('.layui-layer-setwin .layui-layer-max').css("display", "none")
                    // $('.layui-layer-setwin .layui-layer-min').css("display", "none")
                    break;
            }
            ;
        });

        //监听单元格编辑
        table.on('edit(menuTables)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/system/updateMenuByTid",
                dataType: "json",
                async: false,
                data: "tid="+data.tid+"&sort="+value,
                success: function (msg) {

                }
            })
        });

        function update(data){
            var clocess="1";
            var index = layer.open({
                title : "修改",
                type: 2,
                content:  '<%=request.getContextPath()%>/rest/getRoute?type=system&url=menu_update',
                //是否显示右上角关闭图标，默认为1，显示
                maxmin: true,
                area: ['90%', '91%'],
                //是否显示右上角关闭图标，默认为1，显示
                anim:4,
                shade:0.6,
                skin: 'layui-layer-molv',
                resize:true,
                success: function(layero, index){  //当你需要在层创建完毕时即执行一些语句，可以通过该回调 想相当于吧值转到子界面
                    // var body = layer.getChildFrame('body',index);//建立父子联系  //当你试图在当前页获取iframe页的DOM元素时，你可以用此方法。
                    var iframe = window['layui-layer-iframe'+index];
                    //调用子页面的全局函数
                    iframe.child(data.tid)
                },cancel: function(index, layero) {
                    layer.close(index)
                    clocess="2"
                    return false;
                },
                end: function () {
                    if(clocess=="2"){

                    }else {
                        renderTable();
                    }
                    return false;
                }
            });
        }


        function del(data,obj) {
            layer.confirm('确定删除此数据', {icon: 3, title: '提示信息', shade: 0.6}, function (index) {
                layer.close(index);
                layer.msg('删除中，请稍候', {icon: 16, time: false, shade: 0.6});
                setTimeout(function () {
                    //向服务端发送删除指令
                    $.ajax({
                        type: "post",
                        url: "<%=request.getContextPath()%>/rest/system/delMenu",
                        data: "tid=" + data.tid,
                        dataType: "json",
                        async: false,
                        success: function (msg) {
                            layer.close(index);
                            if (msg == true) {
                                layer.msg('删除成功');
                                obj.del();
                            } else {
                                layer.msg(msg.errorMsg);
                            }
                        }
                    })
                }, 2000);
            });
        }

    });
</script>
</body>
</html>